<?php
 /*****************************************************************************
 * viewContact.php                                                            *
 * Last Modified: 2007-06-07                                                  *
 *                                                                            *
 * viewContact.php outputs the data stored in the contact record selected     *
 * from contacts.php. Allows user to print the record.                        *
 *                                                                            *
 * BBG_Billing, a PHP application using MySQL for creating and maintaining a  *
 * contacts and invoices database.                                            *
 *                                                                            *
 * @copyright Copyright (C) 2007, Bugs Bee Gone Computer Services             *
 * @owner     Daniel Barnett (Bugs Bee Gone Computer Services)                *
 * @author    Daniel Barnett <dbarnett@bugsbeegone.com>                       *
 * @website   http://www.bugsbeegone.com                                      *
 * @license   http://www.gnu.org/licenses/gpl.html GNU General Public License *
 * @package   BBG_Billing                                                     *
 * @name      viewContact.php                                                 *
 * @version   1.0.0                                                           *
 * @uses      Jpmaster77's Login Script (Written by: Jpmaster77, 2004-08-19)  *
 *             ->(http://www.evolt.org/PHP-Login-System-with-Admin-Features/) *
 *                                                                            *
 * This file is part of the "BBG_Billing" PHP application.                    *
 *                                                                            *
 * BBG_Billing is free software; you can redistribute it and/or modify        *
 * it under the terms of the GNU General Public License as published by       *
 * the Free Software Foundation; either version 2 of the License, or          *
 * (at your option) any later version.                                        *
 *                                                                            *
 * BBG_Billing is distributed in the hope that it will be useful,             *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 * GNU General Public License for more details.                               *
 *                                                                            *
 * You should have received a copy of the GNU General Public License          *
 * along with BBG_Billing; if not, write to the Free Software                 *
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
 *****************************************************************************/


    // Require session.php for user access control
    require "include/session.php";

    // If user is not logged in, redirect to main.php
    if(!$session->logged_in){
        header("Location: main.php");
        exit();
    }

    // Require config.inc.php for configuration variables
    require_once "config.inc.php";

    // Connect to MySQL
    mysql_connect($path, $username, $password);
    // Select the database, if error die with error message
    @mysql_select_db($database) or die("Unable to select database.<br />" . mysql_error());

    // Get key value from URL
    $key = "";
    $key = $_GET['key'];

    // Get customerID value from URL
    $customerID = "";
    $customerID = $_GET['customerID'];

    // Determine if key is NULL, if it is...
    if($key == ""  && $customerID == "") {
        // Die with error message
        die('Cannot retrieve the contract without the record ID or customer ID. Please <a href="contacts.php">try again</a>.');
    // If key is not NULL...
    } else {
        // If key is not NULL and customerID is NULL...
        if($key != "" && $customerID == "") {
            // Define MySQL query to select the contact record
            $query = "SELECT * FROM `tblContacts` WHERE `key` = $key";
        } else {
            // If customerID is not NULL and key is NULL...
            if($customerID != "" && $key == "") {
                // Define MySQL query to select the contact record
                $query = "SELECT * FROM `tblContacts` WHERE `customerID` = $customerID";
            // All others redirect to contacts.php
            } else {
                header("Location: contacts.php");
                exit();
            }  // End if($customerID != "" && $key == "")
        }  // End if($key != "" && $customerID == "")
    }  // End if($key == ""  && $customerID == "")

        // Execute the query, if error die with error message
        $result = mysql_query($query) or die(mysql_error());

        // Determine number of rows returned by MySQL query
        $num = mysql_numrows($result);

        // If no rows were returned by the MySQL query...
        if($num == 0) {
            // Die with error message
            die('Record not found. Please check the record ID and <a href="contacts.php">try again</a>.');
        // If rows were returned by the MySQL query...
        } else {
            // Define data variables
            $key = mysql_result($result,0,"key");
            $customerID = mysql_result($result,0,"customerID");
            $company = mysql_result($result,0,"company");
            $firstName = mysql_result($result,0,"firstName");
            $lastName = mysql_result($result,0,"lastName");
            $address1 = mysql_result($result,0,"address1");
            $address2 = mysql_result($result,0,"address2");
            $city = mysql_result($result,0,"city");
            $state = mysql_result($result,0,"state");
            $zipCode = mysql_result($result,0,"zipCode");
            $phone1 = mysql_result($result,0,"phone1");
            $phone2 = mysql_result($result,0,"phone2");
            $fax = mysql_result($result,0,"fax");
            $email = mysql_result($result,0,"email");
            $webSite = mysql_result($result,0,"webSite");
            $notes = mysql_result($result,0,"notes");
        }

    // Close the connection
    mysql_close();

echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' . "\n";

echo '<html xmlns="http://www.w3.org/1999/xhtml">' . "\n";
	echo '<head>' . "\n";
		echo '<meta name="ROBOTS" content="NOODP, NOFOLLOW" />' . "\n";
		echo '<meta name="GOOGLEBOT" content="NOODP, NOFOLLOW" />' . "\n";
		echo '<title>Bugs Bee Gone - Billing - ' . $firstName . ' ' . $lastName . '</title>' . "\n";
		echo '<meta name="description" content="Bugs Bee Gone - Billing - View Contact" />' . "\n";
		echo '<link rel="shortcut icon" href="" />' . "\n";
		echo '<link href="contact.css" type="text/css" rel="stylesheet" />' . "\n";

		echo '<script type="text/javascript" src="print_page.js"></script>' . "\n";
		echo '<script type="text/javascript">' . "\n";
		echo 'function printPage() {' . "\n";
		echo 'createPage(document.getElementById("contact").innerHTML, "http://www.bugsbeegone.com", "Bugs Bee Gone - Billing - ' . $firstName . ' ' . $lastName . '", "");' . "\n";
		echo '}' . "\n";
		echo '</script>' . "\n";

		echo '<script type="text/javascript" src="func.inc.js"></script>' . "\n";
	echo '</head>' . "\n";

	echo '<body>' . "\n";
?>

	<div id="contact">
		<table width="300px" border="0" align="center">
		<tr>
			<td valign="top" width="100%" style="word-wrap: break-word;">
				<?php echo "<b>Customer ID:</b> " . $customerID; ?><br />&nbsp;<br />

				<?php
                            // If company is not NULL, output company
                            if($company != "") {
                                echo $company . '<br />' . "\n";
                            }
                        ?>

				<?php echo $firstName . " " . $lastName; ?><br />
				<?php echo $address1; ?><br />

                        <?php
                            // If address2 is not NULL, output address2
                            if($address2 != "") {
                                echo $address2 . '<br />' . "\n";
                            }
                        ?>

				<?php echo $city . ", " . $state . " " . $zipCode; ?><br />&nbsp;<br />
				<?php echo "Phone: " . $phone1; ?><br />

                        <?php
                            // If phone2 is not NULL, output phone2
                            if($phone2 != "") {
                                echo "Phone: " . $phone2 . '<br />' . "\n";
                            }
                        ?>

                        <?php
                            // If fax is not NULL, output fax
                            if($fax != "") {
                                echo "Fax: " . $fax . '<br />' . "\n";
                            }
                        ?>

                        <?php
                            // If email is not NULL, output email
                            if($email != "") {
                                echo '<a href="mailto:' . $email . '">' . $email . '</a><br />' . "\n";
                            }
                        ?>

                        <?php  // If webSite is not NULL, output webSite
                            if($webSite != "") {
                                echo '<a href="' . $webSite . '" target="_blank">' . $webSite . '</a><br />' . "\n";
                            }
                        ?>

				<br /><b><u>Notes:</u></b><br />
                        <?php
                            // If notes is not NULL, output notes
                            if($notes != "") {
                                echo $notes . '<br />' . "\n";
                            } else {
                                echo "&nbsp;&bull; No notes to display.<br />\n";
                            }
                        ?>
			</td>
		</tr>

            <tr>
                  <td width="100%">
                      <br /><b><u>Files:</u></b><br />
                      <table width="100%" style="border-style:none; text-align:center;">
                          <?php
                              // Connect to MySQL
                              mysql_connect($path, $username, $password);
                              // Select the database, if error die with error message
                              @mysql_select_db($database) or die("Unable to select database.<br />" . mysql_error());

                              // Define MySQL query to select the files associated with this contact
                              $fileQuery = "SELECT `key`, `name` FROM tblSysInfo WHERE `customerID` = $customerID";

                              // Execute the MySQL query, if error die with error message
                              $result2 = mysql_query($fileQuery) or die('Error, query failed<br />' . mysql_error());

                              // If no rows were returned by the MySQL query, output message...
                              if(mysql_numrows($result2) == 0) {
                                  echo "<tr>\n<td>No files to display.</td>\n</tr>\n";
                              // If rows were returned by the MySQL query, output the rows...
                              } else {
                                  while(list($key, $name) = mysql_fetch_array($result2)) {
                          ?>
                                  <tr>
                                  <td>&bull; <a href="download.php?id=<?php echo $key; ?>"><?php echo $name; ?></a></td>
                                  </tr>
                          <?php
                                  }  // End while(list($key, $name) = mysql_fetch_array($result))
                              }  // End if(mysql_num_rows($result) == 0)

                              // Close the connection
                              mysql_close();
                          ?>
                      </table>
                  </td>
            </tr>

		<tr>
			<td valign="middle" width="100%" style="text-align:center;">
				<br /><input type="button" onclick="printPage()" value="Print" name="printButton" /> <input type="button" onclick="cancel(this)" location="contacts.php" value="Cancel" />
			</td>
		</tr>
		</table>
	</div>

	</body>
</html>
